

const firsturl=[
  'userPage/faultScan',
  'userPage/userInfo',
  'userPage/scan',
  'userPage/nearby'
];
const filter_url=[
  'activate',
  'findPwd',
  'userPage'
];

const register_url=[
    '/userPage/registerStep1',
    '/userPage/registerStep2'
];
export default {
  //百度坐标转高德（传入经度、纬度）
      bd_decrypt:function (bd_lng, bd_lat) {
        let X_PI = Math.PI * 3000.0 / 180.0;
        let x = bd_lng - 0.0065;
        let y = bd_lat - 0.006;
        let z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * X_PI);
        let theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * X_PI);
        let gg_lng = z * Math.cos(theta);
        let gg_lat = z * Math.sin(theta);
        return {lng: gg_lng, lat: gg_lat}
      },
      //高德坐标转百度（传入经度、纬度）
      bd_encrypt:function(gg_lng, gg_lat) {
        let X_PI = Math.PI * 3000.0 / 180.0;
        let x = gg_lng, y = gg_lat;
        let z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * X_PI);
        let theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * X_PI);
        let bd_lng = z * Math.cos(theta) + 0.0065;
        let bd_lat = z * Math.sin(theta) + 0.006;
        return {
          bd_lat: bd_lat,
          bd_lng: bd_lng
        };
      },

    filterPath: function (toPath) {
        //debugger;
        let belong = false ;
        // 循环过滤路由，判断是否路由属于要过滤的路由
        for (let i=0; i<filter_url.length; i++){
          if(toPath.indexOf(filter_url[i]) > 0 ){
            belong = true;
            return belong
          }
        }
        return belong;
    },
    filterFirstPath: function (toPath) {
      //debugger;
      let belong = false ;
      // 循环过滤路由，判断是否路由属于要过滤的路由
      for (let i=0; i<firsturl.length; i++){
        if(toPath.indexOf(firsturl[i]) > 0 ){
          belong = true;
          return belong
        }
      }
      return belong;
    },
    filterRegisterPath: function (toPath) {
        //debugger;
        let belong = false ;
        register_url.forEach(element => {
            if(element === toPath) belong = true;
        });
        return belong;
    },
    //获取时间差，获得时分秒
    GetDateDiff: function (startTime, endTime, diffType) {
        //将xxxx-xx-xx的时间格式，转换为 xxxx/xx/xx的格式
        startTime = startTime.replace(/\-/g, "/");
        endTime = endTime.replace(/\-/g, "/");
        //将计算间隔类性字符转换为小写
        diffType = diffType.toLowerCase();
        var sTime =new Date(startTime); //开始时间
        var eTime =new Date(endTime); //结束时间
        //作为除数的数字
        var timeType =1;
        var microsecond = eTime.getTime() - sTime.getTime();
        var timeDay = parseInt(microsecond / parseInt(1000*60*60*24));
        var timeHour = parseInt((microsecond % parseInt(1000*60*60*24)) / parseInt(1000*60*60));
        var timeMinute = parseInt((microsecond % parseInt(1000*60*60)) / parseInt(1000*60));
        var timeSecond = parseInt((microsecond % parseInt(1000*60)) / parseInt(1000));
        switch (diffType) {
            case"second":
                return timeSecond;
            break;
            case"minute":
                return timeMinute;
            break;
            case"hour":
                return timeHour;
            break;
            case"day":
                return timeDay;
            break;
            default:
            break;
        }
    },
    //验证密码是否是6-16位
    verifyPwd: function (_this,pwd) {
        if(!(/^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,16}$/.test(pwd))){
            _this.$vux.toast.text('密码为6-16位字母和数字组合');
            return false
        }else{
            return true;
        }
    },
    //验证手机号是否是11位数字
    verifyTel: function (_this,tel) {
        if(!(/^1[3|4|5|7|8][0-9]\d{4,8}$/.test(tel))){
            _this.$vux.toast.text('输入手机格式不正确');
            return false
        }else{
            return true;
        }
    },
    //验证码格式是否正确
    verifyCaptcha: function (_this,captcha) {
        if(!(/^\d{6}$/.test(captcha))){
            _this.$vux.toast.text('输入验证码格式不正确');
            return false
        }else{
            return true;
        }
    },
    //验证两个密码是否相等
    verifyEqual: function (_this,pwd1,pwd2) {
        if(pwd1!==pwd2){
            _this.$vux.toast.text('两次输入密码的不相等');
            return false
        }else{
            return true;
        }
    },

    nextDay:function(dateStr){    //生成下一天日期字符串
        let currDate = new Date(dateStr);
        let currHours = currDate.getHours();
        let nextHours = currDate.setHours(currHours + 24);
        let nextDay = new Date(nextHours);
        //获取完整的年份(4位,1970-????)
        let nextDayYear = (nextDay.getFullYear()).toString();
        //获取当前月份(0-11,0代表1月)
        let nextDayMonth = nextDay.getMonth()+1<10 ? "0"+(nextDay.getMonth()+1).toString():(nextDay.getMonth()+1).toString();
        //获取当前日(1-31)
        let nextDayDay = nextDay.getDate()<10?"0"+(nextDay.getDate()).toString():(nextDay.getDate()).toString();
        return nextDayYear+"-"+nextDayMonth+"-"+nextDayDay;
    },
    prevDay:function(dateStr){    //生成上一天日期字符串
        let currDate = new Date(dateStr);
        let currHours = currDate.getHours();
        let prevHours = currDate.setHours(currHours - 24);
        let prevDay = new Date(prevHours);
        //获取完整的年份(4位,1970-????)
        let prevDayYear = (prevDay.getFullYear()).toString();
        //获取当前月份(0-11,0代表1月)
        let prevDayMonth = prevDay.getMonth()+1<10 ? "0"+(prevDay.getMonth()+1).toString():(prevDay.getMonth()+1).toString();
        //获取当前日(1-31)
        let prevDayDay = prevDay.getDate()<10?"0"+(prevDay.getDate()).toString():(prevDay.getDate()).toString();
        return prevDayYear+"-"+prevDayMonth+"-"+prevDayDay;
    },
    getToDay:function(){    //生成今日日期字符串
        let currDate = new Date();
        //获取完整的年份(4位,1970-????)
        let currDateYear = (currDate.getFullYear()).toString();
        //获取当前月份(0-11,0代表1月)
        let currDateMonth = currDate.getMonth()+1<10 ? "0"+(currDate.getMonth()+1).toString():(currDate.getMonth()+1).toString();
        //获取当前日(1-31)
        let currDateDay = currDate.getDate()<10?"0"+(currDate.getDate()).toString():(currDate.getDate()).toString();
        return currDateYear+"-"+currDateMonth+"-"+currDateDay;
    },
    // 通过url获取companyCode
    getCompanyCode:function(url){
      if (this.filterFirstPath(url)) {
        // 将原url使用‘/’分割
        let urls = url.split('/')
        // 取出最后的companyCode
        return urls[urls.length-1]
      }else{
        return ""
      }
    },
    // 将取出cookies，双引号改成单引号
    doubleToSingleQuote:function(cookiesStr){
      let resultStr = cookiesStr;
      // 如果resultStr不为空
      if (resultStr) {
        let aaa =resultStr.slice(0,resultStr.length-1);
        // 去掉双引号，加上双引号
        resultStr = "\'"+resultStr.slice(0,resultStr.length-1)+"\'";
      }
      // 返回值
      return resultStr
    },
    dateFormat:function(fmt, date){
        let ret;
        const opt = {
            "Y+": date.getFullYear().toString(),        // 年
            "m+": (date.getMonth() + 1).toString(),     // 月
            "d+": date.getDate().toString(),            // 日
            "H+": date.getHours().toString(),           // 时
            "M+": date.getMinutes().toString(),         // 分
            "S+": date.getSeconds().toString()          // 秒
            // 有其他格式化字符需求可以继续添加，必须转化成字符串
        };
        for (let k in opt) {
            ret = new RegExp("(" + k + ")").exec(fmt);
            if (ret) {
                fmt = fmt.replace(ret[1], (ret[1].length == 1) ? (opt[k]) : (opt[k].padStart(ret[1].length, "0")))
            };
        };
        return fmt;
    },
    wait:function(ms) {
        return new Promise(resolve=>setTimeout(()=>resolve(), ms));
    }
}

